package com.hjq.fcloud.utils

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.widget.TextView
import com.hjq.fcloud.R
import com.hjq.fcloud.ktx.ViewKtx.setVisibility

object DrawableUtils {

    /**
     * 根据文字颜色设置样式
     */
    fun setTextStyleByStyle(textView: TextView?, text:String?, color:String?){
        if(textView == null || text == null || color == null){
            return
        }

        var context = textView.context
        var realColor = color
        //如果包含#则先截取
        if(realColor.startsWith("#")){
            realColor = realColor.substring(1,realColor.length)
        }
        //如果有8位，表示有透明度，则需去除透明度
        if(realColor.length == 8){
            realColor = realColor.substring(2,realColor.length)
        }
        var drawable = getDrawableByColor(context,realColor)
        textView.setVisibility(true)
        textView.setBackgroundDrawable(drawable)
        textView.setTextColor(Color.parseColor("#".plus(realColor)))
        textView.setText(text)
        var l = context.resources.getDimensionPixelOffset(R.dimen.dp_8)
        var t = context.resources.getDimensionPixelOffset(R.dimen.dp_3)
        textView.setPadding(l, t, l, t)
    }

    fun getDrawableByColor(context: Context, color: String): GradientDrawable {
        var realColor = "#10".plus(color)//10是透明度
        //改变背景色
        var drawable = GradientDrawable();
        drawable.setShape(GradientDrawable.RECTANGLE)
        drawable.setCornerRadius(context.resources.getDimension(R.dimen.common_button_radius))
        drawable.setColor(Color.parseColor(realColor))
        return drawable
    }

}